﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;

namespace FirstWPFApp
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        //int frequncy,time;
        //TextBox frequncyBox,timeBox;
        //Thread _beep;
        #region 字段
        /// <summary>
        /// 表示频率
        /// </summary>
        int frequncy = 100;
        /// <summary>
        /// 表示时间
        /// </summary>
        int time = 1000;
        /// <summary>
        /// 表示时间的文本框的实例
        /// </summary>
        TextBox frequncyBox;
        /// <summary>
        /// 表示时间的文本框的实例
        /// </summary>
        TextBox timeBox;
        /// <summary>
        /// 响铃线程
        /// </summary>
        Thread _beep;
        #endregion

        public MainWindow()
        {
            InitializeComponent();
        }

        #region 播放及响铃
        private void Play(object sender, RoutedEventArgs e)
        {
            Thread _Beep = new Thread(new ThreadStart(Beep));
            _beep = _Beep;
            _Beep.Start();
        }

        /// <summary>
        /// 响铃
        /// </summary>
        private void Beep()
        {
            try
            {
                Console.Beep(frequncy, time);
            }
            catch(ArgumentOutOfRangeException)
            {
                MessageBox.Show("频率需要在37Hz~32767Hz之间");
            }
        }
        #endregion

        #region 清除文本
        private void FrequncyClear(object sender, RoutedEventArgs e)
        {
            frequncyBox.Text = "";
        }

        private void TimeClear(object sender, RoutedEventArgs e)
        {
            timeBox.Text = "";
        }
        #endregion

        #region 时间、频率改变
        private void FrequncyChanged(object sender, TextChangedEventArgs e)
        {
            TextBox tbox = null;
            try
            {
                tbox = (TextBox)sender;
                frequncy = Convert.ToInt32(tbox.Text);
            }
            catch (Exception)
            { }
            frequncyBox = tbox;
        }

        private void TimeChanged(object sender, TextChangedEventArgs e)
        {
            TextBox tbox = null;
            try
            {
                tbox = (TextBox)sender;
                time = Convert.ToInt32(tbox.Text);
            }
            catch (Exception)
            { }
            timeBox = tbox;
        }
        #endregion

        #region 停止和退出
        private void Exit(object sender, RoutedEventArgs e)
        {
            Environment.Exit(0);
        }

        private void Stop(object sender, RoutedEventArgs e)
        {
            Console.Beep();
            _beep.Abort();
        }
        #endregion

        #region + -按钮点击事件
        private void FrequncyAdd100(object sender, RoutedEventArgs e)
        {
            try
            {
                frequncyBox.Text = Convert.ToString(Convert.ToInt32(frequncyBox.Text) + 100);
            }
            catch (Exception) { }
        }

        private void TimeAdd100(object sender, RoutedEventArgs e)
        {
            try
            {
                timeBox.Text = Convert.ToString(Convert.ToInt32(timeBox.Text) + 100);
            }
            catch (Exception) { }
        }

        private void FrequncyMinus100(object sender, RoutedEventArgs e)
        {
            try
            {
                frequncyBox.Text = Convert.ToString(Convert.ToInt32(frequncyBox.Text) - 100);
            }
            catch (Exception) { }
        }

        private void TimeMinus100(object sender, RoutedEventArgs e)
        {
            try
            {
                timeBox.Text = Convert.ToString(Convert.ToInt32(timeBox.Text) - 100);
            }
            catch (Exception) { }
        }
        #endregion

        #region 按序扫描
        private void Scan1(object sender, RoutedEventArgs e)
        {
            _beep = new Thread(new ThreadStart(Beep1));
            _beep.Start();
        }

        private void Scan2(object sender, RoutedEventArgs e)
        {
            _beep = new Thread(new ThreadStart(Beep2)); ;
            _beep.Start();
        }

        private void Beep1()
        {
            for (int i = 370; i <= 32767; i += 100)
                Console.Beep(i, 100);
        }

        private void Beep2()
        {
            for (int i = 32767; i >= 0; i -= 100)
                Console.Beep(i, 100);
        }
        #endregion
    }
}
